/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: line.js 5 2012-03-09 10:44:54Z CC.HGFeng@gmail.com $
 */

/**
 * OTR.Graph.PolyLine
 * 多段直线对象
 * 
 * @class OTR.Graph.PolyLine
 * @constructor
 * @param object params
 * dx  连结终点横坐标
 * dy  连结终点纵坐标
 */
OTR.Graph.PolyLine = OTR.extend(OTR.Graph.Shape, {

    /**
     * 初始化对象
     * 
     * @param object params
     */
    _init: function(params) {
        this._addAttrs({points: [], endArrow: null, startArrow: null});
        this._setName('PolyLine');
        
        OTR.Graph.Rect.__super._init.call(this, params);
    },
    
    /**
     * 设置当前对象属性
     * 
     * @override
     * @param object params
     */
    setAttrs: function(params) {
       for (var i in params) {
           if (undefined === this._attrs[i]) {
               continue ;
           }
           
           switch (i) {
               case 'points':
                   this.setPoints(params[i]);
                   break;
               default: 
                   this._attrs[i] = params[i];
                   break;
           }
       }
    },

    /**
     * 线路顶点 
     * 
     * @param array points
     * @return NTR.Graph.Polygon
     */
    setPoints: function(points) {
        if (!OTR.Util.isArray(points) || points.length < 3) {
            throw 'invalid point list';
        }
        
        var i = 0, k = points.length, pl = [];
        for ( ; i < k; i++) {
            var pair = points[i];
            if (OTR.Util.isArray(pair)) {
                if (pair.length >= 2) {
                    pl.push({x: pair[0], y: pair[1]});
                }
            } else if (OTR.Util.isObject(pair)) {
                if (undefined !== pair.x && undefined !== pair.y) {
                    pl.push({x: pair.x, y: pair.y});
                }
            }
        }

        this._attrs.points = pl;
    }
});